﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers.WebApi
{
    public class CostRegisterApiController : ApiController
    {
        /// <summary>
        /// 费用登记服务层
        /// </summary>
        private readonly CostRegisterService service = new CostRegisterService();

        /// <summary>
        /// 费用登记
        /// </summary>
        /// <param name="ent">实体</param>
        /// <returns></returns>
        [HttpPost]
        public object SaveCostRegister([FromBody]CostRegister ent)
        {
            return service.Add(ent);
        }
        /// <summary>
        /// 费用登记
        /// </summary>
        /// <param name="ent">实体</param>
        /// <returns></returns>
        [HttpPost]
        public object SaveUpCostRegister([FromBody]CostRegister ent)
        {
            return service.Update(ent);
        }
        /// <summary>
        /// 获取用车申请列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        [HttpPost]
        public object GetPageData()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int pageSize = Convert.ToInt32(HttpContext.Current.Request["pageSize"]);
            int pageIndex = Convert.ToInt32(HttpContext.Current.Request["pageIndex"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var tmp = service.GetPageData(pageSize, pageIndex, id);
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
    }
}
